<template>
  <div
    :class="className"
    :style="{height:height,width:width,border:'solid 1px #ccc',padding:'10px',marginBottom:'10px',margin:'0 auto',background:'#fff'} "
    :chartData="chartData"
  />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils/debounce";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "80%"
    },
    height: {
      type: String,
      default: "484px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.initChart();
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize();
        }
      }, 100);
      window.addEventListener("resize", this.__resizeHandler);
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName("sidebar-container")[0];
    this.sidebarElm &&
      this.sidebarElm.addEventListener(
        "transitionend",
        this.sidebarResizeHandler
      );
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    if (this.autoResize) {
      window.removeEventListener("resize", this.__resizeHandler);
    }

    this.sidebarElm &&
      this.sidebarElm.removeEventListener(
        "transitionend",
        this.sidebarResizeHandler
      );

    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === "width") {
        this.__resizeHandler();
      }
    },
    setOptions({ title,month, data } = {}) {
      var end;
      var num = 10;
      if (data.length <= num) {
        end = 100;
      } else {
        end = Math.floor((num / data.length) * 100);
      }
      this.chart.setOption({
        title: {
          text: title
        },
        tooltip: {
          trigger: "axis",
          formatter: function(params) {
            return (
              // params[0].axisValue
              month+
              params[0].axisValue + "日" +
              "</br>" +
              "人数 : " +
              data[params[0].dataIndex].num +
              "</br>" 
            );
          }
        },
        xAxis: {
          name: "日期",
          data: data.map(function(item) {
            return item.time;
          }),
          axisLabel: {
            rotate: 30
          }
        },

        yAxis: {
          name: "访问量",
          type: "value",
          splitLine: {
            show: true
          }
        },

        dataZoom: {
          type: "slider",
          start: 0,
          end: end
        },
        visualMap: {
          top: 10,
          right: 10,
          pieces: [
            {
              gt: 0,
              lte: 50,
              color: "#096"
            },
            {
              gt: 50,
              lte: 100,
              color: "#ffde33"
            },
            {
              gt: 100,
              lte: 150,
              color: "#ff9933"
            },
            {
              gt: 150,
              lte: 200,
              color: "#cc0033"
            },
            {
              gt: 200,
              lte: 300,
              color: "#660099"
            },
            {
              gt: 300,
              color: "#7e0023"
            }
          ],
          outOfRange: {
            color: "#999"
          }
        },
        series: {
          name: "访问量",
          type: "line",
          data: data.map(function(item) {
            return item.num;
          }),
        //   markLine: {
        //     silent: true,
        //     data: [
        //       {
        //         yAxis: 50
        //       },
        //       {
        //         yAxis: 100
        //       },
        //       {
        //         yAxis: 150
        //       },
        //       {
        //         yAxis: 200
        //       },
        //       {
        //         yAxis: 300
        //       }
        //     ]
        //   }
        }
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    }
  }
};
</script>
